package com.dingreading.cloud.soms.util;

import com.dingreading.cloud.common.dto.AliyunBookDto;
import com.dingreading.cloud.common.dto.BookSimpleDto;
import com.dingreading.cloud.common.util.aliyun.AliyunBookUtil;
import com.dingreading.cloud.soms.entity.Books;
import com.dingreading.cloud.soms.entity.StoreBooks;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 书籍相关工具
 */
public class BooksUtil {

    /**
     * 获取书籍分类
     */
    public static Map<String, Long> getBookLabelMap() {
        Map<String, Long> map = new HashMap<>();
        map.put("审美", 1L);
        map.put("勇敢", 2L);
        map.put("乐观", 3L);
        map.put("善良", 4L);
        map.put("宽容", 5L);
        map.put("自立", 6L);
        map.put("勤奋", 7L);
        map.put("创新", 8L);
        return map;
    }

    /**
     * 新建门店书籍实体
     *
     * @param books   书籍
     * @param storeId 门店id
     * @param time    时间
     * @param source  来源
     */
    public static StoreBooks newStoreBooks(Books books, Long storeId, Date time, Integer source) {
        StoreBooks storeBooks = new StoreBooks();
        storeBooks.setStoreId(storeId);
        storeBooks.setJournalId(books.getJournalId());
        storeBooks.setJournalNum(books.getJournalNum());
        storeBooks.setJournalName(books.getJournalName());
        storeBooks.setBookLabelId(books.getBookLabelId());
        storeBooks.setBookLabelName(books.getBookLabelName());
        storeBooks.setBookTypeId(books.getBookTypeId());
        storeBooks.setBookType(books.getBookType());
        storeBooks.setBooksId(books.getId());
        storeBooks.setBookNum(books.getBookNum());
        storeBooks.setIsbn(books.getIsbn());
        storeBooks.setBookName(books.getBookName());
        storeBooks.setImg(books.getImg());
        storeBooks.setAuthor(books.getAuthor());
        storeBooks.setPress(books.getPress());
        storeBooks.setPubDate(books.getPubDate());
        storeBooks.setSynopsis(books.getSynopsis());
        storeBooks.setIntroduction(books.getIntroduction());
        storeBooks.setUnitPrice(books.getUnitPrice());
        storeBooks.setStockCount(1);
        storeBooks.setBorrowCount(1);
        storeBooks.setSource(source);
        storeBooks.setStatus(books.getStatus());
        storeBooks.setAddTime(time);
        storeBooks.setEditTime(time);
        return storeBooks;
    }

    public static List<BookSimpleDto> getBookSimpleDto(List<Books> booksList) {
        List<BookSimpleDto> bookSimpleDtoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(booksList)) {
            bookSimpleDtoList = booksList.stream().map(books ->
            {
                BookSimpleDto bookSimpleDto = new BookSimpleDto();
                bookSimpleDto.setType(2);
                bookSimpleDto.setStockCount(0);
                bookSimpleDto.setBooksId(books.getId());
                bookSimpleDto.setIsbn(books.getIsbn());
                bookSimpleDto.setBookNum(books.getBookNum());
                bookSimpleDto.setBookName(books.getBookName());
                bookSimpleDto.setAuthor(books.getAuthor());
                bookSimpleDto.setBookLabelId(books.getBookLabelId());
                bookSimpleDto.setBookLabelName(books.getBookLabelName());
                bookSimpleDto.setBookType(books.getBookType());
                return bookSimpleDto;
            }).collect(Collectors.toList());
        }
        return bookSimpleDtoList;
    }

    public static List<BookSimpleDto> getBookSimpleDtoByStoreBook(List<StoreBooks> storeBooksList) {
        List<BookSimpleDto> bookSimpleDtoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(storeBooksList)) {
            bookSimpleDtoList = storeBooksList.stream().map(storeBooks ->
            {
                BookSimpleDto bookSimpleDto = new BookSimpleDto();
                bookSimpleDto.setType(1);
                bookSimpleDto.setStockCount(storeBooks.getStockCount());
                bookSimpleDto.setStoreBooksId(storeBooks.getId());
                bookSimpleDto.setBooksId(storeBooks.getBooksId());
                bookSimpleDto.setIsbn(storeBooks.getIsbn());
                bookSimpleDto.setBookNum(storeBooks.getBookNum());
                bookSimpleDto.setBookName(storeBooks.getBookName());
                bookSimpleDto.setAuthor(storeBooks.getAuthor());
                bookSimpleDto.setBookLabelId(storeBooks.getBookLabelId());
                bookSimpleDto.setBookLabelName(storeBooks.getBookLabelName());
                bookSimpleDto.setBookType(storeBooks.getBookType());
                return bookSimpleDto;
            }).collect(Collectors.toList());
        }
        return bookSimpleDtoList;
    }

    public static BookSimpleDto newBookSimpleDto(Long bookId, Long storeBookId, Integer type, Integer count, String isbn,
                                                 String bookNum, String bookName, String author, Long labelId, String labelName,
                                                 String bookType) {
        BookSimpleDto bookSimpleDto = new BookSimpleDto();
        bookSimpleDto.setType(type);
        bookSimpleDto.setStockCount(count);
        bookSimpleDto.setStoreBooksId(storeBookId);
        bookSimpleDto.setBooksId(bookId);
        bookSimpleDto.setIsbn(isbn);
        bookSimpleDto.setBookNum(bookNum);
        bookSimpleDto.setBookName(bookName);
        bookSimpleDto.setAuthor(author);
        bookSimpleDto.setBookLabelId(labelId);
        bookSimpleDto.setBookLabelName(labelName);
        bookSimpleDto.setBookType(bookType);
        return bookSimpleDto;
    }

    // 阿里云新增总部系统书籍
    public static Books aliNewBooks(AliyunBookDto.ResultDTO resultDto, String isbn, String bookNum, String bookName,
                                    String press, String pubDate, String author, Double price, Long storeId, Date time) {
        //新增书籍
        Books books = new Books();

        if (StringUtils.isNotBlank(bookName)) {
            books.setBookName(bookName);
        } else {
            books.setBookName(resultDto.getTitle());
        }

        if (StringUtils.isNotBlank(press)) {
            books.setPress(press);
        } else {
            books.setPress(resultDto.getPublisher());
        }

        if (StringUtils.isNotBlank(pubDate)) {
            books.setPubDate(pubDate);
        } else {
            books.setPubDate(resultDto.getPubdate());
        }

        if (StringUtils.isNotBlank(author)) {
            books.setAuthor(author);
        } else {
            books.setAuthor(resultDto.getAuthor());
        }

        if (price != null) {
            books.setUnitPrice(price);
        } else {
            price = AliyunBookUtil.getPrice(resultDto.getPrice());
            books.setUnitPrice(price);
        }
        books.setIsbn(isbn);
        books.setBookNum(bookNum);
        books.setImg(resultDto.getPic());
        books.setSynopsis(resultDto.getSummary());
        books.setSource(2);
        books.setStatus(1);
        books.setSourceStoreId(storeId);
        books.setAddTime(time);
        books.setEditTime(time);
        return books;
    }

}
